<?php

// +----------------------------------------------------------------------
// | TAODUDU.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------

namespace Admin\Controller;

use Admin\Controller\AdminController;

class NodeController extends AdminController {

    function _initialize() {
        parent::_initialize();
        $this->assign('action', 'node');
        $this->assign('sys', 'dashboard'); // 数据集
    }

    function qx() {
        $rid = I('get.id');
        if (IS_POST) {
            $access = array();
            //组合权限
            foreach (I('access', array()) as $v) {
                $tmp = explode('_', $v);
                $access[] = array('role_id' => $rid, 'node_id' => $tmp[0], 'level' => $tmp[1]);
            }
            //清空原权限
            M('access')->where(array('role_id' => $rid))->delete();
            if (empty($access)) {
                $this->success('配置成功', U('Role/index'));
            }
            //插入新权限
            //mysql,支持addAll
            $ret = 0;
            if (in_array(strtolower(C('DB_TYPE')), array('mysql', 'mysqli', 'mongo'))) {
                $ret = M('access')->addAll($access);
            } else {
                foreach ($access as $v) {
                    $ret = M('access')->add($access);
                }
            }

            if ($ret) {
                $this->success('添加成功', U('Role/index'));
            } else {
                $this->error('添加失败');
            }
        } else {
            $access = M('access')->where(array('role_id' => $rid))->getField('node_id', true);
            if (!$access) {
                $access = array();
            }
            $where = array('status' => 1);
            $node = M('node')->where($where)->order('sort')->select();
            $node = node2layer($node, $access);
            $this->assign('node', $node);
            $this->assign('rid', $rid);
            $this->display();
        }
    }

    //节点列表
    public function index() {
        $node = M('node')->order('sort')->select();
        $node = node2layer($node);
        $this->assign('node', $node);
        $this->display();
    }

    //添加管理员
    public function add() {

        $pid = I('get.pid');
        $level = I('get.level');
        if (empty($pid)) {
            $pid = 0;
        } else {
            $pid = $pid;
        }
        if (IS_POST) {
            $date['name'] = I('name');
            $date['title'] = I('title');
            $date['status'] = I('status');
            $date['sort'] = I('sort');
            $date['pid'] = $pid;
            $date['level'] = $level;
            $re = M('node')->add($date);
            if ($re) {
                $this->success("节点添加成功！", U('index'));
            } else {
                $this->error("添加节点出错");
            }
        } else {
            $this->display();
        }
    }

    //修改管理员
    function edit() {
        $id = I('id');
        $uModel = M('node');
        if (empty($id) | $id <= 0) {
            $this->error('非法的操作', U('index'));
        }
        if (IS_POST) {
            $date['name'] = I('name');
            $date['title'] = I('title');
            $date['status'] = I('status');
            $date['sort'] = I('sort');
            $resule = $uModel->where('id=%d', $id)->save($date);
            if (false !== $resule) {
                //记录操作日志
                $this->success('修改成功！正在跳转……', U('index'));
            } else {
                $this->error('修改失败~~~ 正在返回……');
            }
        } else {
            $date = $uModel->where('id=%d', $id)->find();
            $this->assign('date', $date);
            $this->display();
        }
    }

    function del() {

        $u_id = I('post.data');
        $uModel = M('node');
        $name = $uModel->where('id=%d', $u_id)->getField('title');
        if ($uModel->where('id=%d', $u_id)->delete()) {
            //记录操作日志
            $this->success('删除成功！正在跳转……');
        } else {
            $this->error('删除失败！正在返回……');
        }
    }

}
